package com.rojored.layouts.box2DLayoutClasses
{

import Box2DAS.Common.V2;
import Box2DAS.Common.b2Def;
import Box2DAS.Dynamics.b2Body;
import Box2DAS.Dynamics.b2World;

/**
 *  Simple physics correlate that uses a single box.
 */
public class BoxPhysicsCorrelate extends PhysicsCorrelateBase
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function BoxPhysicsCorrelate()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    private var box:b2Body;
    
    /**
     *  @private
     */
    private var groupIndex:int;

    //--------------------------------------------------------------------------
    //
    //  Overriden properties: PhysicsCorrelateBase
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    //   centerX
    //--------------------------------------

    /**
     *  @inheritDoc
     */
    override public function get centerX():Number
    {
        // TOOD: perhaps we could manage these three values as one AS3
        // Vector3D
        if (box)
            return box.GetPosition().x;

        return super.centerX;
    }

    //--------------------------------------
    //   centerY
    //--------------------------------------

    /**
     *   @inheritDoc
     */
    override public function get centerY():Number
    {
        if (box)
            return box.GetPosition().y;

        return super.centerY;
    }

    //--------------------------------------
    //   angle
    //--------------------------------------

    /**
     *  @inheritDoc
     */
    override public function get angle():Number
    {
        if (box)
            return box.GetAngle();

        return super.angle;
    }

    //--------------------------------------------------------------------------
    //
    //  Overriden methods: PhysicsCorrelateBase
    //
    //--------------------------------------------------------------------------

    /**
     *  @inheritDoc
     */
    override public function initialize(world:b2World,
                                        groupIndex:int, centerX:Number,
                                        centerY:Number, angle:Number, width:Number,
                                        height:Number):void
    {
        b2Def.body.type = b2Body.b2_dynamicBody;
        // FIXME: double-check
        b2Def.body.allowSleep = false;
        b2Def.body.position.x = centerX;
        b2Def.body.position.y = centerY;
        b2Def.body.angle = angle;
        b2Def.polygon.SetAsBox(width / 2, height / 2);
        b2Def.fixture.density = 0.7;
        b2Def.fixture.filter.groupIndex =  groupIndex;

        this.groupIndex = groupIndex;

        box = world.CreateBody(b2Def.body);
        box.CreateFixture(b2Def.fixture)
        bodies.push(box);

        _width = width;
        _height = height;
        _centerX = centerX;
        _centerY = centerY;
        _angle = angle;

    }
    
    /**
     *   @inheritDoc
     */
    override public function setPosition(centerX:Number, centerY:Number,
                                        angle:Number):void
    {
        super.setPosition(centerX, centerY, angle);
        if (!box)
            return;

        box.SetTransform(new V2(centerX, centerY), angle);
        box.SetAngularVelocity(0);
        box.SetLinearVelocity(new V2(0, 0));
    }
    
    /**
     *   @inheritDoc
     */
    override public function setSize(width:Number, height:Number):void
    {
        super.setSize(width, height);
        if (!box)
            return;

        box.DestroyFixture(box.GetFixtureList());
        b2Def.polygon.SetAsBox(width / 2, height / 2);
        b2Def.fixture.shape = b2Def.polygon;
        b2Def.fixture.filter.groupIndex =  groupIndex;
        box.CreateFixture(b2Def.fixture);
        box.SetTransform(box.GetPosition(), box.GetAngle());
            
    }
}
}
